import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import QuestionDetail from '../views/QuestionDetail.vue'
import PublishQuestion from '../views/PublishQuestion.vue'

const routes = [
    { path: '/login', component: Login },
    { path: '/', component: Home },
    { path: '/question/:id', component: QuestionDetail },
    { path: '/publish', component: PublishQuestion }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    const user = localStorage.getItem('user')
    if (to.path === '/publish' && !user) {
        next('/login')
    } else {
        next()
    }
})

export default router
